/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.community.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Directory;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.common.service.CommentService;
import etcore.webapp.grouptalk.common.service.MessageService;
import etcore.webapp.grouptalk.common.service.TopicService;
import etcore.webapp.grouptalk.user.object.Member;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.MemberService;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * This action validates the request and executes the command of showing
 * community groups,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class CommunityAction extends AbstractAction {

	private static final long serialVersionUID = 559780093605442405L;

	// validate result
	public static final int SUCCESS = 1;

	// utility constants

	public static final String GID = "gid";

	public static final String MSGS = "msgs";
	public static final String GROUP_MSG_COUNT = "groupMsgCount";
	public static final String GROUP_COMMENT_COUNT = "groupCommentCount";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

		return SUCCESS;
	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {

		return PageConstants.PAGE_PATH_COMMUNITY;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		String directoriesKey = PageConstants.DIRECTORIES2;
		String rootGroupsKey = PageConstants.ROOTGROUPS2;
		String nav = request.getParameter(PageConstants.NAV);
		if (PageConstants.ONE.equals(nav)) {
			request.setAttribute(PageConstants.NAV, PageConstants.ONE);
			directoriesKey = PageConstants.DIRECTORIES22;
			rootGroupsKey = PageConstants.ROOTGROUPS22;
		}
		List<Directory> directories = (List<Directory>) request
				.getAttribute(directoriesKey);

		List<Group> rootGroups = (List<Group>) request
				.getAttribute(rootGroupsKey);
		if (directories == null && rootGroups == null)
			return;

		int msgCount = 5;
		String msgCountStr = request.getParameter("msgCount");
		if (msgCountStr != null && !Symbols.BLANK.equals(msgCountStr)) {
			msgCount = Integer.parseInt(msgCountStr);
		}
		// enable banner or not
		String hasBanner = request.getParameter(PageConstants.HASBANNER);
		if (Messages.FALSE.equals(hasBanner))
			request.setAttribute(PageConstants.HASBANNER, false);
		else
			request.setAttribute(PageConstants.HASBANNER, true);

		// enable footer or not
		String hasFooter = request.getParameter(PageConstants.HASFOOTER);
		if (Messages.FALSE.equals(hasFooter))
			request.setAttribute(PageConstants.HASFOOTER, false);
		else
			request.setAttribute(PageConstants.HASFOOTER, true);
		TopicService topicService = new TopicService();
		MessageService msgService = new MessageService();
		CommentService commentService = new CommentService();
		Map<Integer, String> topicMap = new HashMap<Integer, String>();
		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute(PageConstants.SESSIONUSER);
		for (Directory directory : directories) {
			List<Group> groups = (List<Group>) request.getAttribute(directory
					.getId() + PageConstants.GROUPS);

			for (Group group : groups) {

				long groupMsgCount = msgService.findSizeByGid(group.getId())
						+ commentService.searchSizeByGid(group.getId());
				long todayGroupMsgCount = msgService.findSizeByDaysAndGid(1,
						group.getId())
						+ commentService.searchSizeByGidAndDays(group.getId(),
								1);
				request.setAttribute(group.getId() + GROUP_MSG_COUNT,
						groupMsgCount);
				request.setAttribute(group.getId() + GROUP_COMMENT_COUNT,
						todayGroupMsgCount);

				if (user != null) {
					MemberService memberService = new MemberService();
					Member member = memberService.findByUsernameAndGid(
							user.getUserName(), group.getId());
					if (member != null)
						request.setAttribute("included" + group.getId(), true);
					else
						request.setAttribute("included" + group.getId(), false);
				}
				if (group.getFunc() == 0) {
					List<Topic> topics = topicService.findByProperty(
							PageConstants.GID, new Integer(group.getId()));
					request.setAttribute(group.getId() + PageConstants.TOPICS,
							topics);
					for (Topic topic : topics) {
						topicMap.put(topic.getId(), topic.getName());
					}

					@SuppressWarnings("rawtypes")
					List msgs = msgService.findTopByDaysAndGid(
							new Integer(2000), group.getId(), 0, msgCount);
					request.setAttribute(group.getId() + PageConstants.MSGS,
							msgs);

				}
			}
		}
		for (Group group : rootGroups) {

			if (user != null) {
				MemberService memberService = new MemberService();
				Member member = memberService.findByUsernameAndGid(
						user.getUserName(), group.getId());
				if (member != null)
					request.setAttribute("included" + group.getId(), true);
				else
					request.setAttribute("included" + group.getId(), false);
			}
			long groupMsgCount = msgService.findSizeByGid(group.getId());
			// long todayGroupMsgCount = msgService
			// .findSizeByDaysAndGid(1, group.getId())
			// + commentService.searchSizeByGidAndDays(
			// group.getId(), 1);
			request.setAttribute(group.getId() + GROUP_MSG_COUNT, groupMsgCount);
			request.setAttribute(group.getId() + GROUP_COMMENT_COUNT,
					commentService.searchSizeByGid(group.getId()));

			if (group.getFunc() == 0) {
				List<Topic> topics = topicService.findByProperty(
						PageConstants.GID, new Integer(group.getId()));
				request.setAttribute(group.getId() + PageConstants.TOPICS,
						topics);
				for (Topic topic : topics) {
					topicMap.put(topic.getId(), topic.getName());
				}

				@SuppressWarnings("rawtypes")
				List msgs = msgService.findTopByDaysAndGid(new Integer(2000),
						group.getId(), 0, msgCount);
				request.setAttribute(group.getId() + PageConstants.MSGS, msgs);

			}
		}
		request.setAttribute(PageConstants.TOPIC_MAP, topicMap);

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#
	 * buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
